SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE PROCEDURE dbo.[DnnProduct_EnsureLocalizationExists]
	@PortalId       int,
	@CultureCode	nvarchar(10)
AS
	IF NOT EXISTS (SELECT * FROM dbo.DnnProduct_PortalLocalization WHERE CultureCode=@CultureCode AND Portalid=@PortalId)
		BEGIN
			DECLARE @PortalName nvarchar(128)
			DECLARE @LogoFile nvarchar(50)
			DECLARE @FooterText nvarchar(100)
			DECLARE @Description nvarchar(500)
			DECLARE @KeyWords nvarchar(500)
			DECLARE @BackgroundFile nvarchar(50)
			DECLARE @HomeTabId int
			DECLARE @LoginTabId int
			DECLARE @UserTabId int
			DECLARE @AdminTabId int
			DECLARE @RegisterTabId int
			DECLARE @SearchTabId int
	
			DECLARE @defaultlanguage nvarchar(10)
			
			SELECT @defaultlanguage= DefaultLanguage FROM dbo.DnnProduct_Portals WHERE Portalid=@PortalId

			IF EXISTS (SELECT * FROM dbo.DnnProduct_PortalLocalization WHERE CultureCode=@defaultlanguage AND Portalid=@PortalId)
					AND EXISTS (SELECT * FROM dbo.DnnProduct_Languages l INNER JOIN dbo.DnnProduct_PortalLanguages pl ON l.LanguageID = pl.LanguageID WHERE pl.PortalID = @PortalID AND l.CultureCode = @CultureCode )
				BEGIN
					--clone the default language
					SELECT 
						@PortalName = PortalName,
						@LogoFile = LogoFile,
						@FooterText = FooterText,
						@Description = Description,
						@KeyWords = KeyWords,
						@BackgroundFile = BackgroundFile,
						@HomeTabId = HomeTabId,
						@LoginTabId = LoginTabId,
						@UserTabId = UserTabId,
						@AdminTabId = AdminTabId,
						@SearchTabId = SearchTabId,
						@RegisterTabId = RegisterTabId 
					FROM dbo.DnnProduct_PortalLocalization WHERE CultureCode=@defaultlanguage AND Portalid=@PortalId
						
					INSERT INTO dbo.DnnProduct_PortalLocalization (
						PortalId,
						CultureCode,
						PortalName,
						LogoFile,
						FooterText,
						Description,
						KeyWords,
						BackgroundFile, 
						HomeTabId,
						LoginTabId,
						UserTabId,
						AdminTabId,
						RegisterTabId,
						SearchTabId,
						CreatedByUserID,
						CreatedOnDate,
						LastModifiedByUserID,
						LastModifiedOnDate
						) 
						VALUES	(
							@PortalId,
							@CultureCode,
							@PortalName,
							@LogoFile,
							@FooterText,
							@Description,
							@KeyWords,
							@BackgroundFile,
							@HomeTabId,
							@LoginTabId,
							@UserTabId,
							@AdminTabId,
							@RegisterTabId,
							@SearchTabId,
							-1,
							getdate(),
							-1,
							getdate()
						)
				END
			ELSE
				BEGIN
					IF EXISTS (select * from dbo.DnnProduct_PortalLocalization where CultureCode='en-us' and Portalid=@PortalId)
						BEGIN
							--if defaultlanguage is missing clone en-us if available
							SELECT 
								@PortalName = PortalName,
								@LogoFile = LogoFile,
								@FooterText = FooterText,
								@Description = Description,
								@KeyWords = KeyWords,
								@BackgroundFile = BackgroundFile,
								@HomeTabId = HomeTabId,
								@LoginTabId=LoginTabId,
								@UserTabId=UserTabId,
								@AdminTabId=AdminTabId,
								@SearchTabId=SearchTabId,
								@RegisterTabId=RegisterTabId 
							FROM dbo.DnnProduct_PortalLocalization where CultureCode='en-us' and Portalid=@PortalId
							
							INSERT INTO dbo.DnnProduct_PortalLocalization (
								PortalId,
								CultureCode,
								PortalName,
								LogoFile,
								FooterText,
								Description,
								KeyWords,
								BackgroundFile, 
								HomeTabId,
								LoginTabId,
								UserTabId,
								AdminTabId,
								RegisterTabId,
								SearchTabId,
								CreatedByUserID,
								CreatedOnDate,
								LastModifiedByUserID,
								LastModifiedOnDate
							) 
							VALUES	(
								@PortalId,
								@CultureCode,
								@PortalName,
								@LogoFile,
								@FooterText,
								@Description,
								@KeyWords,
								@BackgroundFile,
								@HomeTabId,
								@LoginTabId,
								@UserTabId,
								@AdminTabId,
								@RegisterTabId,
								@SearchTabId,
								-1,
								getdate(),
								-1,
								getdate()
							)
						END
					ELSE
						BEGIN
							IF EXISTS (select top 1 * from dbo.DnnProduct_PortalLocalization where  Portalid=@PortalId)
								BEGIN
									--cannot select by particular culturecode as defaultlanguage has been deleted (possible in early versions of DotNetNuke)
									SELECT TOP 1 
										@PortalName = PortalName,
										@LogoFile = LogoFile,
										@FooterText = FooterText,
										@Description = Description,
										@KeyWords = KeyWords,
										@BackgroundFile = BackgroundFile,
										@HomeTabId = HomeTabId,
										@LoginTabId=LoginTabId,
										@UserTabId=UserTabId,
										@AdminTabId=AdminTabId,
										@SearchTabId=SearchTabId,
										@RegisterTabId=RegisterTabId 
									FROM dbo.DnnProduct_PortalLocalization where portalid=@PortalId
									
									INSERT INTO dbo.DnnProduct_PortalLocalization (
										PortalId,
										CultureCode,
										PortalName,
										LogoFile,
										FooterText,
										Description,
										KeyWords,
										BackgroundFile, 
										HomeTabId,
										LoginTabId,
										UserTabId,
										AdminTabId,
										RegisterTabId,
										SearchTabId,
										CreatedByUserID,
										CreatedOnDate,
										LastModifiedByUserID,
										LastModifiedOnDate
									) 
									VALUES	(
										@PortalId,
										@CultureCode,
										@PortalName,
										@LogoFile,
										@FooterText,
										@Description,
										@KeyWords,
										@BackgroundFile,
										@HomeTabId,
										@LoginTabId,
										@UserTabId,
										@AdminTabId,
										@RegisterTabId,
										@SearchTabId,
										-1,
										getdate(),
										-1,
										getdate()
									)
								END
						END	
				END
		END
GO
